
package com.dotools.utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class DevicesUtils_HuaWei {
    public static boolean isHuaWeiRom() {
        return Build.MANUFACTURER.equalsIgnoreCase("HUAWEI") || Build.MANUFACTURER.equalsIgnoreCase("HONOR");
    }

    /**
     * open huawei rom start up
     */
    public static void openAutoHuaweiSetting(Context context) {
        try{
            if (!isHuaWeiRom())
                return;
            Intent intent = new Intent("huawei.intent.action.HSM_BOOTAPP_MANAGER");
            if (Build.VERSION.SDK_INT >= 23) {
                intent.setClassName("com.huawei.systemmanager", "com.huawei.systemmanager.startupmgr.ui.StartupNormalAppListActivity");
            } else {
                intent.setClassName("com.huawei.systemmanager", "com.huawei.systemmanager.optimize.bootstart.BootStartActivity");
            }
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra("extra_pkgname", context.getPackageName());
            context.startActivity(intent);
        }catch (Exception ex){
            if (Build.VERSION.SDK_INT > 25) {
                Intent intent = new Intent("huawei.intent.action.HSM_BOOTAPP_MANAGER");
                intent.setClassName("com.huawei.systemmanager", "com.huawei.systemmanager.appcontrol.activity.StartupAppControlActivity");
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra("extra_pkgname", context.getPackageName());
                context.startActivity(intent);
            }
        }
    }

    /**
     * 打开华为受保护的应用
     */
    public static void openHuaweiProtectActivity(Context context) {
        if (!isHuaWeiRom())
            return;
        Intent localIntent13 = new Intent();
        localIntent13.setClassName("com.huawei.systemmanager", "com.huawei.systemmanager.optimize.process.ProtectActivity");
        localIntent13.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(localIntent13);
    }


    public static void openHuaweiWindowActivity(Context context) {
        if (!isHuaWeiRom())
            return;
        Intent intent = new Intent("huawei.intent.action.NOTIFICATIONMANAGER");
        if (Build.VERSION.SDK_INT >= 23) {
            intent.setClassName("com.huawei.systemmanager",
                    "com.huawei.systemmanager.addviewmonitor.AddViewMonitorActivity");
        } else {
            intent.setClassName("com.huawei.systemmanager",
                    "com.huawei.notificationmanager.ui.NotificationManagmentActivity");
        }
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("extra_pkgname", context.getPackageName());
        if (isIntentAvailable(context, intent)) {
            if (context instanceof Activity) {
                Activity a = (Activity) context;
                a.startActivity(intent);
            }
        }

    }

    @SuppressLint("WrongConstant")
    public static boolean isIntentAvailable(Context context, Intent intent) {
        if (intent == null)
            return false;
        return context.getPackageManager().queryIntentActivities(intent, PackageManager.GET_ACTIVITIES).size() > 0;
    }

    public static String getHWRomVersionCode() {
        try {
            String version = getRom();
            if (version != null) {
                Log.i("HWRom",version);
                String  v= version.split("[.]")[0];
                return v.split("[_]")[1];
            } else {
                return null;
            }
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    private static String getRom() {
        return getSystemProperty("ro.build.version.emui");
    }


    private static String getSystemProperty(String propName) {
        String line;
        BufferedReader input = null;
        try {
            java.lang.Process p = Runtime.getRuntime().exec("getprop " + propName);
            input = new BufferedReader(new InputStreamReader(p.getInputStream()), 1024);
            line = input.readLine();
            input.close();
        } catch (IOException ex) {
            return null;
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                }
            }
        }
        return line;
    }



    /****
     * 跳转华为市场详细APP页面
     * @param act   必须是ACTIVITY
     * @param pkg  APP包名
     */
    public static void jump2HuaweiMarket(Activity act,String pkg){
        Intent intent= new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("appmarket://details?id="+pkg));
        act.startActivity(intent);
    }
}
